package com.jeasonzhao.report.exceptions;

import com.jeasonzhao.commons.utils.Algorithms;
import com.jeasonzhao.report.model.Report;
import com.jeasonzhao.report.msg.LocalizationManager;
import com.jeasonzhao.report.model.ReportParam;
import com.jeasonzhao.report.model.ReportDict;
import com.jeasonzhao.report.model.ReportDatabaseConfiguration;
import com.jeasonzhao.report.model.ReportColumn;
import com.jeasonzhao.commons.xml.XMLNode;
import com.jeasonzhao.commons.json.JSONNode;
import com.jeasonzhao.report.msg.MessageCodes;

public abstract class SQLReportException extends Exception
{
    private static final long serialVersionUID = 1L;
    protected String m_strMessage = null;
    public SQLReportException(Object ...parameter)
    {
        super(getDefaultString(parameter));
        m_strMessage = Algorithms.replaceStringByPosition(
            LocalizationManager.getInstance().get(this.getClass())
            ,toStringArray(parameter)
            );
    }

    public SQLReportException(Throwable t,Object ...parameter)
    {
        super(getDefaultString(parameter),t);
        m_strMessage = Algorithms.replaceStringByPosition(
            LocalizationManager.getInstance().get(this.getClass())
            ,toStringArray(parameter)
            );
    }

    private static String getDefaultString(Object ...parameters)
    {
        StringBuilder buf = new StringBuilder();
        for(int n = 0;null != parameters && n < parameters.length;n++)
        {
            if(null == parameters[n])
            {
                continue;
            }
            buf.append(parameters[n] + " ");
        }
        return buf.toString();
    }

    private static Object[] toStringArray(Object[] parameters)
    {
        Object[] ret = null == parameters ? null : new Object[parameters.length];
        if(null == parameters)
        {
            return ret;
        }
        for(int n = 0;n < parameters.length;n++)
        {
            Object obj = parameters[n];
            if(null == obj)
            {
                ret[n] = null;
            }
            else
            {
                if(Report.class.isAssignableFrom(obj.getClass()))
                {
                    ret[n] = MessageCodes.getDescription((Report) obj);
                }
                else if(ReportParam.class.isAssignableFrom(obj.getClass()))
                {
                    ret[n] = MessageCodes.getDescription((ReportParam) obj);
                }
                else if(ReportDict.class.isAssignableFrom(obj.getClass()))
                {
                    ret[n] = MessageCodes.getDescription((ReportDict) obj);
                }
                else if(ReportDatabaseConfiguration.class.isAssignableFrom(obj.getClass()))
                {
                    ret[n] = MessageCodes.getDescription((ReportDatabaseConfiguration) obj);
                }
                else if(ReportColumn.class.isAssignableFrom(obj.getClass()))
                {
                    ret[n] = MessageCodes.getDescription((ReportColumn) obj);
                }
                else if(XMLNode.class.isAssignableFrom(obj.getClass()))
                {
                    ret[n] = ((XMLNode) obj).toString();
                }
                else if(JSONNode.class.isAssignableFrom(obj.getClass()))
                {
                    ret[n] = ((JSONNode) obj).toString();
                }
                else
                {
                    ret[n] = Algorithms.toString(obj);
                }
            }
        }
        return ret;
    }

    public String getMessage()
    {
        return Algorithms.isEmpty(m_strMessage) ? super.getMessage() : m_strMessage;
    }

    public String toString()
    {
        return getMessage();
    }
}
